<style>
  /* 全局动画 */
  * {
    transition: all 0.3s ease-in-out;
  }

  /* 表单控件动画 */
  .form-control, .form-select {
    transition: all 0.3s ease;
    border: 1px solid #e6e7e9;
  }

  .form-control:focus, .form-select:focus {
    transform: translateY(-2px);
    box-shadow: 0 5px 15px rgba(32, 107, 196, 0.1);
    border-color: #206bc4;
  }

  /* 卡片样式 */
  .card {
    border: none;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    transition: all 0.3s ease;
  }

  .card:hover {
    transform: translateY(-5px);
    box-shadow: 0 15px 30px rgba(0, 0, 0, 0.2);
  }

  /* 按钮动画 */
  .btn {
    transition: all 0.3s ease;
  }

  .btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
  }

  /* 编辑器优化 */
  #edit-article {
    border-radius: 4px;
    overflow: hidden;
    transition: all 0.3s ease;
  }

  #edit-article:hover {
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
  }

  /* 页面标题动画 */
  .page-title {
    position: relative;
    display: inline-block;
  }

  .page-title:after {
    content: '';
    position: absolute;
    width: 100%;
    height: 2px;
    bottom: -5px;
    left: 0;
    background-color: #206bc4;
    transform: scaleX(0);
    transition: transform 0.3s ease;
  }

  .page-title:hover:after {
    transform: scaleX(1);
  }
</style>

<!-- Page header -->
<div class="page-header d-print-none">
  <div class="container-xl">
    <div class="row g-2 align-items-center">
      <div class="col">
        <h2 class="page-title">
          发新贴
        </h2>
      </div>
    </div>
  </div>
</div>
<!-- Page body -->
<div class="page-body">
  <div class="container-xl">
    <div class="card">
      <div class="card-body">
        <div class="mb-3">
          <label class="form-label"><strong>版块</strong></label>
          <select id="article_post_borad" type="text" class="form-select" placeholder="请选择版块">
            <!-- 版块选项将通过JavaScript动态加载 -->
          </select>
        </div>
        <div class="mb-3">
          <label class="form-label"><strong>标题</strong></label>
          <input id="article_post_title" type="text" class="form-control" placeholder="请输入标题">
        </div>
        <div class="mb-3" style="height: 680px;">
          <label class="form-label"><strong>内容</strong></label>
          <div id="edit-article">
            <!-- textarea也是一个表单控件，当在editor.md中编辑好的内容会关联这个文本域上 -->
            <textarea id="article_post_content" style="display: none;"></textarea>
          </div>
        </div>
      </div>
      <div class="card-footer bg-transparent mt-auto">
        <div class="btn-list justify-content-end">
          <a id="article_post_submit" href="javascript:void(0);" class="btn btn-primary">
            <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24" 
              stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
              <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
              <path d="M5 12l5 5l10 -10"></path>
            </svg>
            发 布
          </a>
        </div>
      </div>
    </div>
  </div>
</div>

<!-- 初始化编辑器 -->
<script type="text/javascript">
  $(function () {
    // 动态加载版块列表
    function loadBoardList() {
      $.ajax({
        type: 'GET',
        url: '/board/topList',
        success: function(response) {
          if (response.code == 0) {
            let boardSelect = $('#article_post_borad');
            boardSelect.empty(); // 清空现有选项
            
            // 添加默认选项
            boardSelect.append('<option value="">请选择版块</option>');
            
            // 添加版块选项
            response.data.forEach(function(board) {
              boardSelect.append(`<option value="${board.id}">${board.name}</option>`);
            });
            
            // 添加动画效果
            boardSelect.addClass('animate__animated animate__fadeIn');
            setTimeout(() => {
              boardSelect.removeClass('animate__animated animate__fadeIn');
            }, 500);
          } else {
            $.toast({
              heading: '警告',
              text: response.message,
              icon: 'warning'
            });
          }
        },
        error: function() {
          $.toast({
            heading: '错误',
            text: '加载版块列表失败',
            icon: 'error'
          });
        }
      });
    }

    // 页面加载时获取版块列表
    loadBoardList();

    var editor = editormd("edit-article", {
      width: "100%",
      height: "100%",
      // theme : "dark",
      // previewTheme : "dark",
      // editorTheme : "pastel-on-dark",
      codeFold: true,
      markdown : '', // 处理编辑区内容
      //syncScrolling : false,
      saveHTMLToTextarea: true,    // 保存 HTML 到 Textarea
      searchReplace: true,
      watch : true,                    // 关闭实时预览
      htmlDecode: "style,script,iframe|on*",            // 开启 HTML 标签解析，为了安全性，默认不开启    
      // toolbar  : false,             //关闭工具栏
      // previewCodeHighlight : false, // 关闭预览 HTML 的代码块高亮，默认开启
      emoji: true,
      taskList: true,
      tocm: true,         // Using [TOCM]
      tex: true,                     // 开启科学公式TeX语言支持，默认关闭
      // flowChart: true,               // 开启流程图支持，默认关闭
      // sequenceDiagram: true,         // 开启时序/序列图支持，默认关闭,
      placeholder: '开始创作...',     // 占位符
      path: "./dist/editor.md/lib/"
    });

    // ================== 处理发贴按钮事件 =======================
    $('#article_post_submit').click(function () {
      let boardIdEl = $('#article_post_borad');
      let titleEl = $('#article_post_title');
      let contentEl = $('#article_post_content');
      
      // 非空校验
      if (!boardIdEl.val()) {
        boardIdEl.focus();
        $.toast({
          heading: '提示',
          text: '请选择版块',
          icon: 'warning'
        });
        return;
      }
      
      if (!titleEl.val()) {
        titleEl.focus();
        $.toast({
          heading: '提示',
          text: '请输入帖子标题',
          icon: 'warning'
        });
        return;
      }
      
      if (!contentEl.val()) {
        $.toast({
          heading: '提示',
          text: '请输入帖子内容',
          icon: 'warning'
        });
        return;
      }

      // 构造帖子对象
      let postData = {
        boardId : boardIdEl.val(),
        title : titleEl.val(),
        content : contentEl.val()
      };

      // 提交
      $.ajax({
        type: 'POST',
        url: 'article/creat',
        contentType: 'application/x-www-form-urlencoded',
        data: postData,
        success: function(response) {
          if (response.code == 0) {
            $.toast({
              heading: '成功',
              text: '发帖成功',
              icon: 'success'
            });
            changeNavActive($('#nav_board_index'));
          } else {
            $.toast({
              heading: '警告',
              text: response.message,
              icon: 'warning'
            });
          }
        },
        error: function() {
          $.toast({
            heading: '错误',
            text: '访问出现问题',
            icon: 'error'
          });
        }
      });
    });
  });
</script>


</body>

</html>